package data;

import helpers.StringHelper;

import java.util.ArrayList;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.CategoryModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class CategoryData {
	private Objectify ofy;

	private Cache cache = null;

	private String prefix = "category_";

	private int limit = 20;

	private int page = 1;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public CategoryData() {
		try {
			ObjectifyService.register(CategoryModel.class);
		} catch (Exception e) {
			// e.printStackTrace();
		}

		try {
			cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// e.printStackTrace();
		}

		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}

	public ArrayList<CategoryModel> list() {
		ArrayList<CategoryModel> categories = new ArrayList<CategoryModel>();

		int start = (page - 1) * limit;
		Query<CategoryModel> q = ofy.query(CategoryModel.class).limit(limit)
				.offset(start);
		for (CategoryModel category : q) {
			categories.add(category);
		}

		return categories;
	}

	public ArrayList<CategoryModel> getAll() {
		ArrayList<CategoryModel> categories = new ArrayList<CategoryModel>();

		Query<CategoryModel> q = ofy.query(CategoryModel.class)
				.order("parents");
		for (CategoryModel category : q) {
			categories.add(category);
		}

		return categories;
	}

	public void insert(CategoryModel category) {
		if (cache != null) {
			cache.put(prefix + category.getCategoryId(), category);
		}
		ofy.put(category);
	}

	public void delete(CategoryModel category) {
		if (cache != null
				&& cache.containsKey(prefix + category.getCategoryId())) {
			cache.remove(prefix + category.getCategoryId());
		}

		ofy.delete(category);
	}

	public CategoryModel getCategory(String id) {
		CategoryModel category = new CategoryModel();
		if (cache != null && cache.containsKey(prefix + id)) {
			category = (CategoryModel) cache.get(prefix + id);
		} else {
			try {
				category = ofy.get(new Key<CategoryModel>(CategoryModel.class,
						id));
				cache.put(prefix + id, category);
			} catch (Exception e) {
				category = null;
			}
		}
		return category;
	}

	public CategoryModel getCategoryByAlias(String alias) {
		CategoryModel category = new CategoryModel();
		if (cache != null && cache.containsKey(prefix + alias)) {
			category = (CategoryModel) cache.get(prefix + alias);
		} else {
			try {
				category = ofy.query(CategoryModel.class)
						.filter("alias", alias).get();
				cache.put(prefix + alias, category);
			} catch (Exception e) {
				category = null;
			}
		}
		return category;
	}
}
